/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cmcc.zysoft.infocollection.dao.DepartmentPcDao;
import com.cmcc.zysoft.infocollection.model.Department;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: DepartmentPcService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-5 上午9:47:25
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class DepartmentPcService extends BaseServiceImpl<Department, String> {

	@Resource
	private DepartmentPcDao departmentPcDao;
	
	@Override
	public HibernateBaseDao<Department, String> getHibernateBaseDao() {
		return this.departmentPcDao;
	}
	
	/**
	 * 获取部门树-部门管理列表展示.
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> tree(String companyId) {
		List<Map<String, Object>> list = this.departmentPcDao.tree(companyId, "0");
		return this.getSubDept(companyId, list);
	}
	
	/**
	 * 递归获取子部门列表.
	 * @param companyId 企业ID
	 * @param list 父企业列表
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> getSubDept(String companyId, List<Map<String, Object>> list) {
		for(Map<String, Object> map : list) {
			String haveChildDeparment = map.get("have_child_deparment").toString();
			if("Y".equals(haveChildDeparment)) {
				String parentDepartmentId = map.get("department_id").toString();
				List<Map<String, Object>> childList = this.departmentPcDao.tree(companyId, parentDepartmentId);
				this.getSubDept(companyId, childList);
				map.put("children", childList);
				map.put("state", "closed");
			} else {
				map.put("state", "open");
			}
		}
		return list;
	}
	
	/**
	 * 根据部门名称查询部门信息.
	 * @param deptName 部门名称
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> searchDept(String deptName) {
		Map<String, Object> map = null;
		List<Map<String, Object>> list = this.departmentPcDao.searchDept(deptName);
		List<Map<String, Object>> parentList = new ArrayList<Map<String, Object>>();
		if(null != list && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				map = list.get(i);
				if(!"0".equals(map.get("parent_department_id").toString())) {
					parentList.addAll(this.searchParent(map.get("parent_department_id").toString()));
				}
			}
			list.addAll(parentList);
		}
		return list;
	}
	
	/**
	 * 根据父部门ID递归找出所有部门.
	 * @param parentDepartmentId 父部门ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> searchParent(String parentDepartmentId) {
		List<Map<String, Object>> parentList = this.departmentPcDao.searchDeptByDeptId(parentDepartmentId);
		Map<String, Object> parentMap = parentList.get(0);
		if(!"0".equals(parentMap.get("parent_department_id").toString())) {
			parentList.addAll(this.searchParent(parentMap.get("parent_department_id").toString()));
		}
		return parentList;
	}
	
	/**
	 * 根据企业ID获取其部门信息.
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> deptTree(String companyId) {
		List<Map<String, Object>> list = this.departmentPcDao.deptTree("0", companyId);
		return this.childDept(list, companyId);
	}
	
	/**
	 * 递归获取子部门.
	 * @param list 父部门列表
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> childDept(List<Map<String, Object>> list, String companyId) {
		for(Map<String, Object> map : list) {
			String haveChildDeparment = map.get("haveChildDept").toString();
			if("Y".equals(haveChildDeparment)) {
				String departmentId = map.get("id").toString();
				List<Map<String, Object>> childList = this.departmentPcDao.deptTree(departmentId, companyId);
				this.childDept(childList, companyId);
				map.put("state", "closed");
				map.put("children", childList);
			} else {
				map.put("state", "open");
			}
		}
		return list;
	}
	
	/**
	 * 根据部门ID删除部门,并对其父部门进行相应处理.
	 * @param departmentId 部门ID
	 */
	public void deleteDepartment(String departmentId) {
		Department department = this.departmentPcDao.get(departmentId);
		department.setDelFlag("1");
		this.departmentPcDao.update(department);
		if(!"0".equals(department.getParentDepartmentId())) { //要删除的部门不是顶级父部门
			List<Department> list = this.findByNamedParam(new String[]{"parentDepartmentId", "delFlag"}, new Object[]{department.getParentDepartmentId(), "0"});
			if(null == list || list.isEmpty()) { //当前要删除的部门的父部门不存在子部门,设置父部门不存在子部门标识
				Department parentDept = this.departmentPcDao.get(department.getParentDepartmentId());
				parentDept.setHaveChildDeparment("N");
				this.departmentPcDao.update(parentDept);
			}
		}
	}
	
	/**
	 * 根据部门名称获取部门ID.
	 * @param companyId 企业ID
	 * @param departmentId 部门ID
	 * @param departmentName 部门名称
	 * @return String
	 */
	public String getDepartmentId(String companyId, String departmentId, String departmentName) {
		List<Map<String, Object>> list = this.departmentPcDao.checkDepartmentName(companyId, departmentId, departmentName);
		if(null != list && !list.isEmpty()) {
			return list.get(0).get("department_id").toString();
		} else {
			return "";
		}
	}
	
	/**
	 * 添加部门.
	 * @param companyId 企业ID
	 * @param userId 当前用户ID-企业添加人
	 * @param departmentName 部门名称
	 * @return String(部门ID)
	 */
	@Transactional
	public String addDepartment(String companyId, String userId, String departmentName) {
		Department department = new Department();
		department.setParentDepartmentId("0");
		department.setCompanyId(companyId);
		department.setDepartmentName(departmentName);
		department.setCreateMan(userId);
		department.setCreateTime(new Date());
		department.setDelFlag("0");
		department.setHaveChildDeparment("N");
		return this.departmentPcDao.save(department);
	}
	
	/**
	 * 根据父部门ID和企业ID获取子部门.
	 * @param parentDepartmentId 父部门ID
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> departmentTree(String parentDepartmentId, String companyId) {
		String deptId = "";
		List<Map<String, Object>> depts = this.departmentPcDao.departmentTree(parentDepartmentId, companyId);
		for(Map<String, Object> map : depts) {
			deptId = map.get("id").toString();
			map.put("id", "d_" + deptId);
			map.put("iconCls", "department");
			if("Y".equals(map.get("haveChildDept").toString())) {
				map.put("state", "closed");
			} else {
				map.put("state", "open");
			}
		}
		return depts;
	}
	
	/**
	 * 根据父部门ID获取子部门.
	 * @param parentDepartmentId 父部门ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> departmentTree(String parentDepartmentId) {
		String deptId = "";
		List<Map<String, Object>> depts = this.departmentPcDao.departmentTree(parentDepartmentId);
		for(Map<String, Object> map : depts) {
			deptId = map.get("id").toString();
			map.put("id", "d_" + deptId);
			map.put("iconCls", "department");
			if("Y".equals(map.get("haveChildDept").toString())) {
				map.put("state", "closed");
			} else {
				map.put("state", "open");
			}
		}
		return depts;
	}
}